/**
 * Created by qunxing.du on 2016/8/10.
 * Copyright 2016 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
package com.hw.one.collect.zj.data;

import java.text.ParseException;
import java.util.*;

import com.hw.one.collect.dao.FiveMinDataDao;
import com.hw.one.collect.param.SmallAirParam;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 中建7局5min数据拉取
 */
@Service
public class ZjFiveMinDataService extends ZjDataService {

	@Resource
	private FiveMinDataDao fiveMinDataDao;
	/**
	 * 从数据库拉取数据
	 * 
	 * @return
	 * @throws ParseException
	 */
	private Logger log = Logger.getLogger(this.getClass());

	public List<String> get(Object o) throws ParseException {
		List<String> results = new ArrayList<>();
		java.text.DecimalFormat df = new java.text.DecimalFormat("#0.0");
		java.text.DecimalFormat df1 = new java.text.DecimalFormat("#0.00");

		Map<String, Object> paramMap = new HashMap<>();
		String[] snCodes = o.toString().split(",");
		List<String> snCodeList = new ArrayList<String>();
		Collections.addAll(snCodeList, snCodes);
		paramMap.put("snCodeList", snCodeList);


//		List<Map<String, Object>> res = fiveMinDataDao.findFiveMinData("'".concat(o.toString()).concat("'").replace(",","','")); // 老平台
//		List<Map> res = fiveMinDataDao.findFiveMinDataByNewOne(paramMap); 	// 新平台数据
		SmallAirParam smallAirParam = new SmallAirParam();
		smallAirParam.setSNCodeList(snCodeList);
		List<Map> res = fiveMinDataDao.findRaiseDustFiveMinDataBySNCodeList(smallAirParam);
		if(res.size() == 0)
			return null;
		for(Map<String, Object> re : res){
			String result = null;
			String body = "";
			String[] nn = new String[13];
			String sncode = String.valueOf(re.get("SNCODE"));
			nn[0] = "01";
			nn[1] = "HW";
			nn[2] = "V1.0";
			nn[3] = sncode;
			Map<String, Object> monitorData = (Map<String, Object>) re.get("MONITORDATA");
//			nn[4] = String.valueOf(df.format(monitorData.get("TEMP")));
			if (null == monitorData.get("TEMP")){
				nn[4] = "0";
			}else {
				nn[4] = String.valueOf(df.format(monitorData.get("TEMP")));
			}

//			nn[5] = String.valueOf(df.format(monitorData.get("HUM")));
			if (null == monitorData.get("HUM")) {
				nn[5] = "0";
			}else {
				nn[5] = String.valueOf(df.format(monitorData.get("HUM")));
			}


//			nn[6] = String.valueOf(df1.format(monitorData.get("CTPM25")));

			if (null == monitorData.get("CTPM25")) {
				nn[6] = "0";
			}else {
				nn[6] = String.valueOf(df.format(monitorData.get("CTPM25")));
			}
			if (null == monitorData.get("CTPM10")) {
				nn[7] = "0";
			}else {
				nn[7] = String.valueOf(df.format(monitorData.get("CTPM10")));
			}
//			nn[7] = String.valueOf(df1.format(monitorData.get("CTPM10")));
//			if (Double.parseDouble(nn[7]) == 0)
//				nn[7] = "0";
			
			nn[8] = "0";
			nn[9] = "0";
			nn[10] = "0";
			nn[11] = "0";
			nn[12] = sncode;

			for (int i = 0; i < nn.length; i++) {
				body += (nn[i] + "|");
			}

			String l = String.valueOf(body.substring(0, body.length() - 1).getBytes().length);
			result = "STX" + l + body.trim().substring(0, body.length() - 1) + "ETX" + "\r\n";
			log.info("get date:" + result);
			results.add(result);
		}
		
		
		//System.out.println("get date:" + result);
		
		return results;
	}

	public Date getTimeByMinute(Date date, int minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, minute);
		return calendar.getTime();
	}

}
